package com.jingxi.common.util;


import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class HttpClientUtil {

	public static String doGet(String url, Map<String, String> param) {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		String resultString = "";
		CloseableHttpResponse response = null;
		try {
			URIBuilder builder = new URIBuilder(url);
			if (param != null) {
				for (String key : param.keySet()) {
					builder.addParameter(key, param.get(key));
				}
			}
			URI uri = builder.build();
			// 创建http GET请求
			HttpGet httpGet = new HttpGet(uri);
			//
			response = httpClient.execute(httpGet);
			HttpEntity entity = response.getEntity();
			resultString = EntityUtils.toString(entity, "utf-8");

			response.close();
			httpClient.close();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			return resultString;
		}
	}

	public static String doGet(String url) {
		String string = "";
		try {
			// 创建一个httpclient对象
			CloseableHttpClient httpClient = HttpClients.createDefault();
			// 创建一个GET对象
			HttpGet get = new HttpGet(url);
			// 执行请求
			CloseableHttpResponse response = httpClient.execute(get);
			// 取响应的结果
			int statusCode = response.getStatusLine().getStatusCode();
			response = httpClient.execute(get);
			HttpEntity entity = response.getEntity();
			string = EntityUtils.toString(entity, "utf-8");
			response.close();
			httpClient.close();
			return string;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return string;
	}

	public static String doPost(String url, Map<String, String> param) {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		String resultString = "";
		CloseableHttpResponse response = null;

		try {
			// 创建http GET请求
			HttpPost httpPost = new HttpPost(url);
			List<NameValuePair> kvList = new ArrayList<>();
			for (String key : param.keySet())
				kvList.add(new BasicNameValuePair(key, param.get(key)));
			HttpEntity requestEntity = new UrlEncodedFormEntity(kvList);

			httpPost.setEntity(requestEntity);
			//
			response = httpClient.execute(httpPost);

			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				resultString = EntityUtils.toString(entity, "utf-8");
			}
			response.close();
			httpClient.close();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			return resultString;
		}
	}
}
